home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / mentc.zip / DISPLAY.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  38KB  |  1,726 lines

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. typedef struct    VIDEO {
  16.     int    v_flag;         /* Flags */
  17. #if    COLOR
  18.     int    v_fcolor;        /* current forground color */
  19.     int    v_bcolor;        /* current background color */
  20.     int    v_rfcolor;        /* requested forground color */
  21.     int    v_rbcolor;        /* requested background color */
  22. #endif
  23. #if    INSDEL && MEMMAP == 0
  24.     int    v_rline;        /* requested screen line # */
  25. #endif
  26.     char    v_text[1];        /* Screen data. */
  27. }    VIDEO;
  28.  
  29. #define VFCHG    0x0001            /* Changed flag         */
  30. #define VFEXT    0x0002            /* extended (beyond column 80)    */
  31. #define VFREV    0x0004            /* reverse video status     */
  32. #define VFREQ    0x0008            /* reverse video request    */
  33. #define VFCOL    0x0010            /* color change requested    */
  34.  
  35. static VIDEO   **vscreen;               /* Virtual screen. */
  36. #if    MEMMAP == 0
  37. static VIDEO   **pscreen;               /* Physical screen. */
  38. #endif
  39.  
  40. /*    some local function declarations    */
  41.  
  42. #if    PROTO
  43. #if    MEMMAP
  44. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1);
  45. #else
  46. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1, struct VIDEO *vp2);
  47. #endif
  48. #else
  49. extern PASCAL NEAR updateline();
  50. #endif
  51.  
  52. /*
  53.  * Initialize the data structures used by the display code. The edge vectors
  54.  * used to access the screens are set up. The operating system's terminal I/O
  55.  * channel is set up. All the other things get initialized at compile time.
  56.  * The original window has "WFCHG" set, so that it will get completely
  57.  * redrawn on the first call to "update".
  58.  */
  59.  
  60. PASCAL NEAR vtinit()
  61. {
  62.     register int i;
  63.     register VIDEO *vp;
  64.  
  65.     TTopen();        /* open the screen */
  66.     TTkopen();        /* open the keyboard */
  67.     TTrev(FALSE);
  68.  
  69.  
  70.     /* allocate the virtual screen pointer array */
  71.     vscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  72.     if (vscreen == NULL)
  73.         meexit(1);
  74.  
  75. #if    MEMMAP == 0
  76.     /* allocate the physical shadow screen array */
  77.     pscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  78.     if (pscreen == NULL)
  79.         meexit(1);
  80. #endif
  81.  
  82.     /* for every line in the display */
  83.     for (i = 0; i < term.t_mrow; ++i) {
  84.  
  85.         /* allocate a virtual screen line */
  86.         vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  87.         if (vp == NULL)
  88.             meexit(1);
  89.  
  90.         vp->v_flag = 0;        /* init change clags */
  91. #if    COLOR
  92.         vp->v_rfcolor = 7;    /* init fore/background colors */
  93.         vp->v_rbcolor = 0;
  94. #endif
  95.         /* connect virtual line to line array */
  96.         vscreen[i] = vp;
  97.  
  98. #if    MEMMAP == 0
  99.         /* allocate and initialize physical shadow screen line */
  100.         vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  101.         if (vp == NULL)
  102.             meexit(1);
  103.  
  104.         vp->v_flag = 0;
  105. #if    INSDEL
  106.         vp->v_rline = i;    /* set requested line position */
  107. #endif
  108.         pscreen[i] = vp;
  109. #endif
  110.     }
  111.  
  112.     return (0);
  113. }
  114.  
  115. #if    CLEAN
  116. /* free up all the dynamically allocated video structures */
  117.  
  118. PASCAL NEAR vtfree()
  119. {
  120.     int i;
  121.     for (i = 0; i < term.t_mrow; ++i) {
  122.         free(vscreen[i]);
  123. #if    MEMMAP == 0
  124.         free(pscreen[i]);
  125. #endif
  126.     }
  127.     free(vscreen);
  128. #if    MEMMAP == 0
  129.     free(pscreen);
  130. #endif
  131.     return (0);
  132. }
  133. #endif
  134.  
  135. /*
  136.  * Clean up the virtual terminal system, in anticipation for a return to the
  137.  * operating system. Move down to the last line and clear it out (the next
  138.  * system prompt will be written in the line). Shut down the channel to the
  139.  * terminal.
  140.  */
  141. PASCAL NEAR vttidy()
  142. {
  143.     mlerase();
  144.     movecursor(term.t_nrow, 0);
  145.     TTflush();
  146.     TTclose();
  147.     TTkclose();
  148.  
  149.     return (0);
  150. }
  151.  
  152. /*
  153.  * Set the virtual cursor to the specified row and column on the virtual
  154.  * screen. There is no checking for nonsense values; this might be a good
  155.  * idea during the early stages.
  156.  */
  157. PASCAL NEAR vtmove(row, col)
  158.  
  159. int row, col;
  160.  
  161. {
  162.     vtrow = row;
  163.     vtcol = col;
  164.  
  165.     return (0);
  166. }
  167.  
  168. /* Write a character to the virtual screen. The virtual row and
  169.    column are updated. If we are not yet on left edge, don't print
  170.    it yet. If the line is too long put a "$" in the last column.
  171.    This routine only puts printing characters into the virtual
  172.    terminal buffers. Only column overflow is checked.
  173. */
  174.  
  175. PASCAL NEAR vtputc(c)
  176.  
  177. int c;
  178.  
  179. {
  180.     register VIDEO *vp;    /* ptr to line being updated */
  181.  
  182.     vp = vscreen[vtrow];
  183.  
  184.     if (c == '\t') {
  185.         do {
  186.             vtputc(' ');
  187.         } while (((vtcol + taboff) % (tabsize)) != 0);
  188.     } else if (vtcol >= term.t_ncol) {
  189.         ++vtcol;
  190.         vp->v_text[term.t_ncol - 1] = '$';
  191.     } else if (disphigh && c > 0x7f) {
  192.         vtputc('^');
  193.         vtputc('!');
  194.         c -= 0x80;
  195.         if (c == '\t') {
  196.             vtputc('^');
  197.             vtputc('I');
  198.         } else
  199.             vtputc(c);
  200.     } else if (c < 0x20 || c == 0x7F) {
  201.         vtputc('^');
  202.         vtputc(c ^ 0x40);
  203.     } else {
  204.         if (vtcol >= 0)
  205.             vp->v_text[vtcol] = c;
  206.         ++vtcol;
  207.     }
  208.  
  209.     return (0);
  210. }
  211.  
  212. /*
  213.  * Erase from the end of the software cursor to the end of the line on which
  214.  * the software cursor is located.
  215.  */
  216. PASCAL NEAR vteeol()
  217. {
  218.     register VIDEO    *vp;
  219.  
  220.     vp = vscreen[vtrow];
  221.     while (vtcol < term.t_ncol) {
  222.         if (vtcol >= 0)
  223.         vp->v_text[vtcol] = ' ';
  224.     vtcol++;
  225.     }
  226.  
  227.     return (0);
  228. }
  229.  
  230. /* upscreen:    user routine to force a screen update
  231.         always finishes complete update     */
  232.  
  233. PASCAL NEAR upscreen(f, n)
  234.  
  235. int f,n;    /* prefix flag and argument */
  236.  
  237. {
  238.     update(TRUE);
  239.     return(TRUE);
  240. }
  241.  
  242. /*
  243.  * Make sure that the display is right. This is a three part process. First,
  244.  * scan through all of the windows looking for dirty ones. Check the framing,
  245.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  246.  * correct for the current window. Third, make the virtual and physical
  247.  * screens the same.
  248.  */
  249. PASCAL NEAR update(force)
  250.  
  251. int force;    /* force update past type ahead? */
  252.  
  253. {
  254.     register WINDOW *wp;
  255.  
  256. #if    TYPEAH
  257.     if (force == FALSE && typahead())
  258.         return(TRUE);
  259. #endif
  260. #if    VISMAC == 0
  261.     if (force == FALSE && kbdmode == PLAY)
  262.         return(TRUE);
  263. #endif
  264.  
  265.     /* update any windows that need refreshing */
  266.     wp = wheadp;
  267.     while (wp != NULL) {
  268.         if (wp->w_flag) {
  269.             /* if the window has changed, service it */
  270.             reframe(wp);    /* check the framing */
  271.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  272.                 updone(wp);    /* update EDITed line */
  273.             else if (wp->w_flag & ~WFMOVE)
  274.                 updall(wp);    /* update all lines */
  275.             if (wp->w_flag & WFMODE)
  276.                 modeline(wp);    /* update modeline */
  277.             wp->w_flag = 0;
  278.             wp->w_force = 0;
  279.         }
  280.  
  281. #if    1    /* take this out before the release! */
  282.     if (wp->w_wndp == wheadp) {    /* erroneously circular list */
  283.         wp->w_wndp = (WINDOW *)NULL;
  284.         mlwrite("DAN!!! a bogus circular window list!!!");
  285.         TTgetc();
  286.     }
  287. #endif
  288.         /* on to the next window */
  289.         wp = wp->w_wndp;
  290.     }
  291.  
  292.     /* recalc the current hardware cursor location */
  293.     updpos();
  294.  
  295. #if    MEMMAP
  296.     /* update the cursor and flush the buffers */
  297.     movecursor(currow, curcol - lbound);
  298. #endif
  299.  
  300.     /* check for lines to de-extend */
  301.     upddex();
  302.  
  303.     /* if screen is garbage, re-plot it */
  304.     if (sgarbf != FALSE)
  305.         if (gflags & GFSDRAW)
  306.             sgarbf = FALSE;
  307.         else
  308.             updgar();
  309.  
  310.     /* update the virtual screen to the physical screen */
  311.     updupd(force);
  312.  
  313.     /* update the cursor and flush the buffers */
  314.     movecursor(currow, curcol - lbound);
  315.     TTflush();
  316.  
  317.     return(TRUE);
  318. }
  319.  
  320. /*    reframe:    check to see if the cursor is on in the window
  321.             and re-frame it if needed or wanted        */
  322.  
  323. PASCAL NEAR reframe(wp)
  324.  
  325. WINDOW *wp;
  326.  
  327. {
  328.     register LINE *lp;    /* search pointer */
  329.     register LINE *rp;    /* reverse search pointer */
  330.     register LINE *hp;    /* ptr to header line in buffer */
  331.     register LINE *tp;    /* temp debugging pointer */
  332.     register int i;        /* general index/# lines to scroll */
  333.     register int nlines;    /* number of lines in current window */
  334.  
  335.     /* figure out our window size */
  336.     nlines = wp->w_ntrows;
  337.     if (modeflag == FALSE)
  338.         nlines++;
  339.  
  340.     /* if not a requested reframe, check for a needed one */
  341.     if ((wp->w_flag & WFFORCE) == 0) {
  342.         lp = wp->w_linep;
  343.         for (i = 0; i < nlines; i++) {
  344.  
  345.             /* if the line is in the window, no reframe */
  346.             if (lp == wp->w_dotp)
  347.                 return(TRUE);
  348.  
  349.             /* if we are at the end of the file, reframe */
  350.             if (lp == wp->w_bufp->b_linep)
  351.                 break;
  352.  
  353.             /* on to the next line */
  354.             lp = lforw(lp);
  355.         }
  356.     }
  357.  
  358.     /* reaching here, we need a window refresh */
  359.     i = wp->w_force;
  360.  
  361.     /* if smooth scrolling is enabled,
  362.         first.. have we gone off the top? */
  363.     if (sscroll && ((wp->w_flag & WFFORCE) == 0)) {
  364.         /* search thru the buffer looking for the point */
  365.         tp = lp = rp = wp->w_linep;
  366.         hp = wp->w_bufp->b_linep;
  367.         while ((lp != hp) || (rp != hp)) {
  368.  
  369.             /* did we scroll downward? */
  370.             if (lp == wp->w_dotp) {
  371.                 i = nlines - 1;
  372.                 break;
  373.             }
  374.  
  375.             /* did we scroll upward? */
  376.             if (rp == wp->w_dotp) {
  377.                 i = 0;
  378.                 break;
  379.             }
  380.  
  381.             /* advance forward and back */
  382.             if (lp != hp)
  383.                 lp = lforw(lp);
  384.             if (rp != hp)
  385.                 rp = lback(rp);
  386.  
  387.             /* problems????? */
  388.             if (lp == tp || rp == tp) {
  389.                 mlforce("BUG IN SMOOTH SCROLL--GET DAN!\n");
  390.                 TTgetc();
  391.             }
  392.         }
  393.     /* how far back to reframe? */
  394.     } else if (i > 0) {    /* only one screen worth of lines max */
  395.         if (--i >= nlines)
  396.             i = nlines - 1;
  397.     } else if (i < 0) {    /* negative update???? */
  398.         i += nlines;
  399.         if (i < 0)
  400.             i = 0;
  401.     } else
  402.         i = nlines / 2;
  403.  
  404.     /* backup to new line at top of window */
  405.     lp = wp->w_dotp;
  406.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  407.         --i;
  408.         if (i < 0) {
  409.             mlforce("OTHER BUG IN DISPLAY --- GET DAN!!!\n");
  410.             TTgetc();
  411.         }
  412.         lp = lback(lp);
  413.     }
  414.  
  415.     /* and reset the current line at top of window */
  416.     wp->w_linep = lp;
  417.     wp->w_flag |= WFHARD;
  418.     wp->w_flag &= ~WFFORCE;
  419.     return(TRUE);
  420. }
  421.  
  422. /*    updone: update the current line to the virtual screen        */
  423.  
  424. PASCAL NEAR updone(wp)
  425.  
  426. WINDOW *wp;    /* window to update current line in */
  427.  
  428. {
  429.     register LINE *lp;    /* line to update */
  430.     register int sline;    /* physical screen line to update */
  431.     register int i;
  432.  
  433.     /* search down the line we want */
  434.     lp = wp->w_linep;
  435.     sline = wp->w_toprow;
  436.     while (lp != wp->w_dotp) {
  437.         ++sline;
  438.         lp = lforw(lp);
  439.     }
  440.  
  441.     /* and update the virtual line */
  442.     vscreen[sline]->v_flag |= VFCHG;
  443.     vscreen[sline]->v_flag &= ~VFREQ;
  444.     taboff = wp->w_fcol;
  445.     vtmove(sline, -taboff);
  446.     for (i=0; i < llength(lp); ++i)
  447.         vtputc(lgetc(lp, i));
  448. #if    COLOR
  449.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  450.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  451. #endif
  452.     vteeol();
  453.     taboff = 0;
  454.  
  455.     return (0);
  456. }
  457.  
  458. /*    updall: update all the lines in a window on the virtual screen */
  459.  
  460. PASCAL NEAR updall(wp)
  461.  
  462. WINDOW *wp;    /* window to update lines in */
  463.  
  464. {
  465.     register LINE *lp;    /* line to update */
  466.     register int sline;    /* physical screen line to update */
  467.     register int i;
  468.     register int nlines;    /* number of lines in the current window */
  469.  
  470.     /* search down the lines, updating them */
  471.     lp = wp->w_linep;
  472.     sline = wp->w_toprow;
  473.     nlines = wp->w_ntrows;
  474.     if (modeflag == FALSE)
  475.         nlines++;
  476.     taboff = wp->w_fcol;
  477.     while (sline < wp->w_toprow + nlines) {
  478.  
  479.         /* and update the virtual line */
  480.         vscreen[sline]->v_flag |= VFCHG;
  481.         vscreen[sline]->v_flag &= ~VFREQ;
  482.         vtmove(sline, -taboff);
  483.         if (lp != wp->w_bufp->b_linep) {
  484.             /* if we are not at the end */
  485.             for (i=0; i < llength(lp); ++i)
  486.                 vtputc(lgetc(lp, i));
  487.             lp = lforw(lp);
  488.         }
  489.  
  490.         /* make sure we are on screen */
  491.         if (vtcol < 0)
  492.             vtcol = 0;
  493.  
  494.         /* on to the next one */
  495. #if    COLOR
  496.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  497.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  498. #endif
  499.         vteeol();
  500.         ++sline;
  501.     }
  502.     taboff = 0;
  503.  
  504.     return (0);
  505. }
  506.  
  507. /*    updpos: update the position of the hardware cursor and handle extended
  508.         lines. This is the only update for simple moves.    */
  509.  
  510. PASCAL NEAR updpos()
  511.  
  512. {
  513.     register LINE *lp;
  514.     register int c;
  515.     register int i;
  516.  
  517.     /* find the current row */
  518.     lp = curwp->w_linep;
  519.     currow = curwp->w_toprow;
  520.     while (lp != curwp->w_dotp) {
  521.         ++currow;
  522.         lp = lforw(lp);
  523.     }
  524.  
  525.     /* find the current column */
  526.     curcol = 0;
  527.     i = 0;
  528.     while (i < curwp->w_doto) {
  529.         c = lgetc(lp, i++);
  530.         if (c == '\t')
  531.             curcol += - (curcol % tabsize) + (tabsize - 1);
  532.         else {
  533.             if (disphigh && c > 0x7f) {
  534.                 curcol += 2;
  535.                 c -= 0x80;
  536.             }
  537.             if (c < 0x20 || c == 0x7f)
  538.                 ++curcol;
  539.         }
  540.         ++curcol;
  541.     }
  542.  
  543.     /* adjust by the current first column position */
  544.     curcol -= curwp->w_fcol;
  545.  
  546.     /* make sure it is not off the left side of the screen */
  547.     while (curcol < 0) {
  548.         if (curwp->w_fcol >= hjump) {
  549.             curcol += hjump;
  550.             curwp->w_fcol -= hjump;
  551.         } else {
  552.             curcol += curwp->w_fcol;
  553.             curwp->w_fcol = 0;
  554.         }
  555.         curwp->w_flag |= WFHARD | WFMODE;
  556.     }
  557.  
  558.     /* if horizontall scrolling is enabled, shift if needed */
  559.     if (hscroll) {
  560.         while (curcol >= term.t_ncol - 1) {
  561.             curcol -= hjump;
  562.             curwp->w_fcol += hjump;
  563.             curwp->w_flag |= WFHARD | WFMODE;
  564.         }
  565.     } else {
  566.     /* if extended, flag so and update the virtual line image */
  567.         if (curcol >=  term.t_ncol - 1) {
  568.             vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  569.             updext();
  570.         } else
  571.             lbound = 0;
  572.     }
  573.  
  574.     /* update the current window if we have to move it around */
  575.     if (curwp->w_flag & WFHARD)
  576.         updall(curwp);
  577.     if (curwp->w_flag & WFMODE)
  578.         modeline(curwp);
  579.     curwp->w_flag = 0;
  580.  
  581.     return (0);
  582. }
  583.  
  584. /*    upddex: de-extend any line that derserves it        */
  585.  
  586. PASCAL NEAR upddex()
  587.  
  588. {
  589.     register WINDOW *wp;
  590.     register LINE *lp;
  591.     register int i,j;
  592.     register int nlines;    /* number of lines in the current window */
  593.  
  594.     wp = wheadp;
  595.  
  596.     while (wp != NULL) {
  597.         lp = wp->w_linep;
  598.         i = wp->w_toprow;
  599.         nlines = wp->w_ntrows;
  600.         if (modeflag == FALSE)
  601.             nlines++;
  602.  
  603.         while (i < wp->w_toprow + nlines) {
  604.             if (vscreen[i]->v_flag & VFEXT) {
  605.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  606.                    (curcol < term.t_ncol - 1)) {
  607.                     taboff = wp->w_fcol;
  608.                     vtmove(i, -taboff);
  609.                     for (j = 0; j < llength(lp); ++j)
  610.                         vtputc(lgetc(lp, j));
  611.                     vteeol();
  612.                     taboff = 0;
  613.  
  614.                     /* this line no longer is extended */
  615.                     vscreen[i]->v_flag &= ~VFEXT;
  616.                     vscreen[i]->v_flag |= VFCHG;
  617.                 }
  618.             }
  619.             lp = lforw(lp);
  620.             ++i;
  621.         }
  622.         /* and onward to the next window */
  623.         wp = wp->w_wndp;
  624.     }
  625.  
  626.     return (0);
  627. }
  628.  
  629. /*    updgar: if the screen is garbage, clear the physical screen and
  630.         the virtual screen and force a full update        */
  631.  
  632. PASCAL NEAR updgar()
  633.  
  634. {
  635.     register int i;
  636. #if    MEMMAP == 0
  637.     register int j;
  638.     register char *txt;
  639. #endif
  640.  
  641.     for (i = 0; i < term.t_nrow; ++i) {
  642.         vscreen[i]->v_flag |= VFCHG;
  643. #if    REVSTA
  644.         vscreen[i]->v_flag &= ~VFREV;
  645. #endif
  646. #if    COLOR
  647.         vscreen[i]->v_fcolor = gfcolor;
  648.         vscreen[i]->v_bcolor = gbcolor;
  649. #endif
  650. #if    MEMMAP == 0
  651.         txt = pscreen[i]->v_text;
  652.         for (j = 0; j < term.t_ncol; ++j)
  653.             txt[j] = ' ';
  654. #endif
  655.     }
  656.  
  657.     movecursor(0, 0);         /* Erase the screen. */
  658.     (*term.t_eeop)();
  659.     sgarbf = FALSE;          /* Erase-page clears */
  660.     mpresf = FALSE;          /* the message area. */
  661. #if    COLOR
  662.     mlerase();            /* needs to be cleared if colored */
  663. #endif
  664.  
  665.     return (0);
  666. }
  667.  
  668. /*    for simple screen size changes (no window re-allocation involved)
  669.     do the following things
  670. */
  671.  
  672. PASCAL NEAR update_size()
  673.  
  674. {
  675.     /* if we need the size update */
  676.     if ((first_screen->s_roworg != term.t_roworg) |
  677.         (first_screen->s_colorg != term.t_colorg) |
  678.         (first_screen->s_nrow != term.t_nrow) |
  679.         (first_screen->s_ncol != term.t_ncol)) {
  680.  
  681.         /* reset the terminal drivers size concept */
  682.         term.t_roworg = first_screen->s_roworg;
  683.         term.t_colorg = first_screen->s_colorg;
  684.         term.t_nrow = first_screen->s_nrow;
  685.         term.t_ncol = first_screen->s_ncol;
  686.  
  687.         /* make sure the update routines know we need a full update */
  688.         sgarbf = TRUE;
  689.     }
  690.  
  691.     return (0);
  692. }
  693.  
  694. /*    Display a pop up window.  Page it for the user.  Any key other
  695.     than a space gets pushed back into the input stream to be interpeted
  696.     later as a command.
  697. */
  698.  
  699. #if    PROTO
  700. int PASCAL NEAR pop(BUFFER *popbuf)
  701. #else
  702. int PASCAL NEAR pop(popbuf)
  703.  
  704. BUFFER *popbuf;
  705. #endif
  706. {
  707.     register int index;    /* index into the current output line */
  708.     register int llen;    /* length of the current output line */
  709.     register int cline;    /* current screen line number */
  710.     LINE *lp;    /* ptr to next line to display */
  711.     int numlines;    /* remaining number of lines to display */
  712.     int c;        /* input character */
  713.  
  714.     /* add the barrior line to the end of the pop up buffer */
  715.     addline(popbuf, "------------------------------------------");
  716.  
  717.     /* set up to scan pop up buffer */
  718.     lp = lforw(popbuf->b_linep);
  719.     mlerase();
  720.     numlines = term.t_nrow-2;
  721.     cline = 0;
  722.  
  723.     while (lp != popbuf->b_linep) {
  724.  
  725.         /* update the virtual screen image for this one line */
  726.         vtmove(cline, 0);
  727.         llen = llength(lp);
  728.         for (index = 0; index < llen; index++)
  729.             vtputc(lp->l_text[index]);
  730.         vteeol();
  731. #if    COLOR
  732.         vscreen[cline]->v_rfcolor = gfcolor;
  733.         vscreen[cline]->v_rbcolor = gbcolor;
  734. #endif
  735.         vscreen[cline]->v_flag &= ~VFREQ;
  736.         vscreen[cline++]->v_flag |= VFCHG|VFCOL;
  737.  
  738.         if (numlines-- < 1) {
  739.  
  740.             /* update the virtual screen to the physical screen */
  741.             updupd(FALSE);
  742.  
  743.             /* tell the user there is more */
  744.             mlwrite("--- more ---");
  745.             TTflush();
  746.  
  747.             /* and see if they want more */
  748.             if ((c = tgetc()) != ' ') {
  749.                 cpending = TRUE;
  750.                 charpending = c;
  751.                 upwind();
  752.                 return(TRUE);
  753.             }
  754.  
  755.             /* reset the line counters */
  756.             numlines = term.t_nrow-2;
  757.             cline = 0;
  758.         }
  759.  
  760.         /* on to the next line */
  761.         lp = lforw(lp);
  762.     }
  763.     if (numlines > 0) {
  764.  
  765.         /* update the virtual screen to the physical screen */
  766.         updupd(FALSE);
  767.         TTflush();
  768.  
  769.         if ((c = tgetc()) != ' ') {
  770.             cpending = TRUE;
  771.             charpending = c;
  772.         }
  773.     }
  774.     upwind();
  775.     return(TRUE);
  776. }
  777.  
  778. /*    updupd: update the physical screen from the virtual screen    */
  779.  
  780. PASCAL NEAR updupd(force)
  781.  
  782. int force;    /* forced update flag */
  783.  
  784. {
  785.     register VIDEO *vp1;
  786.     register int i;
  787.  
  788.     for (i = 0; i < term.t_nrow; ++i) {
  789.         vp1 = vscreen[i];
  790.  
  791.         /* for each line that needs to be updated*/
  792.         if ((vp1->v_flag & VFCHG) != 0) {
  793. #if    TYPEAH
  794.             if (force == FALSE && typahead())
  795.                 return(TRUE);
  796. #endif
  797. #if    MEMMAP
  798.             updateline(i, vp1);
  799. #else
  800.             updateline(i, vp1, pscreen[i]);
  801. #endif
  802.         }
  803.     }
  804.     return(TRUE);
  805. }
  806.  
  807. /*    updext: update the extended line which the cursor is currently
  808.         on at a column greater than the terminal width. The line
  809.         will be scrolled right or left to let the user see where
  810.         the cursor is
  811.                                 */
  812. PASCAL NEAR updext()
  813.  
  814. {
  815.     register int rcursor;    /* real cursor location */
  816.     register LINE *lp;    /* pointer to current line */
  817.     register int j;     /* index into line */
  818.  
  819.     /* calculate what column the real cursor will end up in */
  820.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz)
  821.             + term.t_margin;
  822.     lbound = curcol - rcursor + 1;
  823.     taboff = lbound + curwp->w_fcol;
  824.  
  825.     /* scan through the line outputing characters to the virtual screen */
  826.     /* once we reach the left edge                    */
  827.     vtmove(currow, -taboff); /* start scanning offscreen */
  828.     lp = curwp->w_dotp;        /* line to output */
  829.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  830.         vtputc(lgetc(lp, j));
  831.  
  832.     /* truncate the virtual line, restore tab offset */
  833.     vteeol();
  834.     taboff = 0;
  835.  
  836.     /* and put a '$' in column 1 */
  837.     vscreen[currow]->v_text[0] = '$';
  838.  
  839.     return (0);
  840. }
  841.  
  842. /*
  843.  * Update a single line. This does not know how to use insert or delete
  844.  * character sequences; we are using VT52 functionality. Update the physical
  845.  * row and column variables. It does try an exploit erase to end of line.
  846.  */
  847. #if    MEMMAP
  848. /*    UPDATELINE specific code for memory mapped displays */
  849.  
  850. PASCAL NEAR updateline(row, vp)
  851.  
  852. int row;        /* row of screen to update */
  853. struct VIDEO *vp;    /* virtual screen image */
  854.  
  855. {
  856.     if (vp->v_flag & VFREQ)
  857.         TTrev(TRUE);
  858. #if    COLOR
  859.     scwrite(row, vp->v_text, vp->v_rfcolor, vp->v_rbcolor);
  860.     vp->v_fcolor = vp->v_rfcolor;
  861.     vp->v_bcolor = vp->v_rbcolor;
  862. #else
  863.     scwrite(row, vp->v_text, 7, 0);
  864. #endif
  865.     if (vp->v_flag & VFREQ)
  866.         TTrev(FALSE);
  867.     vp->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  868.  
  869. }
  870.  
  871. #else
  872. PASCAL NEAR updateline(row, vp, pp)
  873.  
  874. int row;        /* row of screen to update */
  875. struct VIDEO *vp;    /* virtual screen image */
  876. struct VIDEO *pp;    /* physical screen image */
  877.  
  878. {
  879.  
  880.     register char *cp1;
  881.     register char *cp2;
  882.     register char *cp3;
  883.     register char *cp4;
  884.     register char *cp5;
  885.     register int nbflag;    /* non-blanks to the right flag? */
  886.     int rev;        /* reverse video flag */
  887.     int req;        /* reverse video request flag */
  888.     int upcol;        /* update column (KRS) */
  889.  
  890.     /* set up pointers to virtual and physical lines */
  891.     cp1 = &vp->v_text[0];
  892.     cp2 = &pp->v_text[0];
  893.  
  894. #if    COLOR
  895.     TTforg(vp->v_rfcolor);
  896.     TTbacg(vp->v_rbcolor);
  897. #endif
  898.  
  899. #if    REVSTA | COLOR
  900.     /* if we need to change the reverse video status of the
  901.        current line, we need to re-write the entire line     */
  902.     rev = (vp->v_flag & VFREV) == VFREV;
  903.     req = (vp->v_flag & VFREQ) == VFREQ;
  904.     if ((rev != req)
  905. #if    COLOR
  906.         || (vp->v_fcolor != vp->v_rfcolor) || (vp->v_bcolor != vp->v_rbcolor)
  907. #endif
  908. #if    HP150
  909.     /* the HP150 has some reverse video problems */
  910.         || req || rev
  911. #endif
  912.             ) {
  913.         movecursor(row, 0);    /* Go to start of line. */
  914.         /* set rev video if needed */
  915.         if (rev != req)
  916.             (*term.t_rev)(req);
  917.  
  918.         /* scan through the line and dump it to the screen and
  919.            the virtual screen array                */
  920.         cp3 = &vp->v_text[term.t_ncol];
  921.         while (cp1 < cp3) {
  922.             TTputc(*cp1);
  923.             ++ttcol;
  924.             *cp2++ = *cp1++;
  925.         }
  926.         /* turn rev video off */
  927.         if (rev != req)
  928.             (*term.t_rev)(FALSE);
  929.  
  930.         /* update the needed flags */
  931.         vp->v_flag &= ~VFCHG;
  932.         if (req)
  933.             vp->v_flag |= VFREV;
  934.         else
  935.             vp->v_flag &= ~VFREV;
  936. #if    COLOR
  937.         vp->v_fcolor = vp->v_rfcolor;
  938.         vp->v_bcolor = vp->v_rbcolor;
  939. #endif
  940.         return(TRUE);
  941.     }
  942. #endif
  943.  
  944.     upcol = 0;
  945.  
  946.     /* advance past any common chars at the left */
  947.     while (cp1 != &vp->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  948.         ++cp1;
  949.         ++upcol;
  950.         ++cp2;
  951.     }
  952.  
  953. #if    DBCS
  954.     /* don't optimize on the left in the middle of a 2 byte char */
  955.     if ((cp1 > &vp->v_text[0]) && is2byte(vp->v_text, cp1 - 1)) {
  956.         --cp1;
  957.         --upcol;
  958.         --cp2;
  959.     }
  960. #endif
  961.  
  962.  
  963. /* This can still happen, even though we only call this routine on changed
  964.  * lines. A hard update is always done when a line splits, a massive
  965.  * change is done, or a buffer is displayed twice. This optimizes out most
  966.  * of the excess updating. A lot of computes are used, but these tend to
  967.  * be hard operations that do a lot of update, so I don't really care.
  968.  */
  969.     /* if both lines are the same, no update needs to be done */
  970.     if (cp1 == &vp->v_text[term.t_ncol]) {
  971.         vp->v_flag &= ~VFCHG;        /* flag this line is changed */
  972.         return(TRUE);
  973.     }
  974.  
  975.     /* find out if there is a match on the right */
  976.     nbflag = FALSE;
  977.     cp3 = &vp->v_text[term.t_ncol];
  978.     cp4 = &pp->v_text[term.t_ncol];
  979.  
  980.     while (cp3[-1] == cp4[-1]) {
  981.         --cp3;
  982.         --cp4;
  983.         if (cp3[0] != ' ')        /* Note if any nonblank */
  984.             nbflag = TRUE;        /* in right match. */
  985.     }
  986.  
  987. #if    DBCS
  988.     /* don't stop in the middle of a 2 byte char */
  989.     if (is2byte(vp->v_text, cp3-1) || is2byte(pp->v_text, cp4-1)) {
  990.         ++cp3;
  991.         ++cp4;
  992.     }
  993. #endif
  994.  
  995.     cp5 = cp3;
  996.  
  997.     /* Erase to EOL ? */
  998.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  999.         while (cp5!=cp1 && cp5[-1]==' ')
  1000.             --cp5;
  1001.  
  1002.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  1003.             cp5 = cp3;        /* fewer characters. */
  1004.     }
  1005.  
  1006.     /* move to the begining of the text to update */
  1007.     movecursor(row, upcol);
  1008.  
  1009. #if    REVSTA
  1010.     if (rev)
  1011.         TTrev(TRUE);
  1012. #endif
  1013.  
  1014.     while (cp1 != cp5) {        /* Ordinary. */
  1015.         TTputc(*cp1);
  1016.         ++ttcol;
  1017.         *cp2++ = *cp1++;
  1018.     }
  1019.  
  1020.     if (cp5 != cp3) {        /* Erase. */
  1021.         TTeeol();
  1022.         while (cp1 != cp3)
  1023.             *cp2++ = *cp1++;
  1024.     }
  1025. #if    REVSTA
  1026.     if (rev)
  1027.         TTrev(FALSE);
  1028. #endif
  1029.     vp->v_flag &= ~VFCHG;        /* flag this line as updated */
  1030.     return(TRUE);
  1031. }
  1032. #endif
  1033.  
  1034. /*
  1035.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  1036.  * only routine that has any idea of how the modeline is formatted. You can
  1037.  * change the modeline format by hacking at this routine. Called by "update"
  1038.  * any time there is a dirty window.
  1039.  */
  1040. PASCAL NEAR modeline(wp)
  1041.  
  1042. WINDOW *wp;    /* window to update modeline for */
  1043.  
  1044. {
  1045.     register char *cp;
  1046.     register int c;
  1047.     register int n;        /* cursor position count */
  1048.     register BUFFER *bp;
  1049.     register int i;        /* loop index */
  1050.     register int lchar;     /* character to draw line in buffer with */
  1051.     register int firstm;    /* is this the first mode? */
  1052.     char tline[NLINE];        /* buffer for part of mode line */
  1053.  
  1054.     /* don't bother if there is none! */
  1055.     if (modeflag == FALSE)
  1056.         return (0);
  1057.  
  1058.     n = wp->w_toprow+wp->w_ntrows;        /* Location. */
  1059.  
  1060. /*
  1061.     Note that we assume that setting REVERSE will cause the terminal
  1062.     driver to draw with the inverted relationship of fcolor and
  1063.     bcolor, so that when we say to set the foreground color to "white"
  1064.     and background color to "black", the fact that "reverse" is
  1065.     enabled means that the terminal driver actually draws "black" on a
  1066.     background of "white".  Makes sense, no?  This way, devices for
  1067.     which the color controls are optional will still get the "reverse"
  1068.     signals.
  1069. */
  1070.  
  1071.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;    /* Redraw next time. */
  1072. #if    COLOR
  1073.     vscreen[n]->v_rfcolor = 7;            /* black on */
  1074.     vscreen[n]->v_rbcolor = 0;            /* white.....*/
  1075. #endif
  1076.     vtmove(n, 0);                /* Seek to right line. */
  1077.     if (wp == curwp)            /* mark the current buffer */
  1078.         lchar = '=';
  1079.     else
  1080. #if    REVSTA
  1081.     if (revexist)
  1082.         lchar = ' ';
  1083.     else
  1084. #endif
  1085.         lchar = '-';
  1086.  
  1087.     bp = wp->w_bufp;
  1088.     if ((bp->b_flag&BFTRUNC) != 0)        /* "#" if truncated */
  1089.         vtputc('#');
  1090.     else
  1091.         vtputc(lchar);
  1092.  
  1093.     if ((bp->b_flag&BFCHG) != 0)        /* "*" if changed. */
  1094.         vtputc('*');
  1095.     else
  1096.         vtputc(lchar);
  1097.  
  1098.     if ((bp->b_flag&BFNAROW) != 0) {        /* "<>" if narrowed */
  1099.         vtputc('<');
  1100.         vtputc('>');
  1101.     } else {
  1102.         vtputc(lchar);
  1103.         vtputc(lchar);
  1104.     }
  1105.  
  1106.     n  = 4;
  1107.     strcpy(tline, " ");             /* Buffer name. */
  1108.     strcat(tline, PROGNAME);
  1109.     strcat(tline, " ");
  1110.     strcat(tline, VERSION);
  1111.     strcat(tline, " ");
  1112.  
  1113.     /* are we horizontally scrolled? */
  1114.     if (wp->w_fcol > 0) {
  1115.         strcat(tline, "[<");
  1116.         strcat(tline, int_asc(wp->w_fcol));
  1117.         strcat(tline, "]");
  1118.     }
  1119.  
  1120.     /* display the modes */
  1121.     strcat(tline, "(");
  1122.     firstm = TRUE;
  1123.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  1124.         if (wp->w_bufp->b_mode & (1 << i)) {
  1125.             if (firstm != TRUE)
  1126.                 strcat(tline, " ");
  1127.             firstm = FALSE;
  1128.             strcat(tline, modename[i]);
  1129.         }
  1130.     strcat(tline,") ");
  1131.  
  1132.     cp = &tline[0];
  1133.     while ((c = *cp++) != 0) {
  1134.         vtputc(c);
  1135.         ++n;
  1136.     }
  1137.  
  1138. #if    0
  1139.     vtputc(lchar);
  1140.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  1141.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  1142.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  1143.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  1144.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  1145.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  1146.     vtputc(lchar);
  1147.     n += 8;
  1148. #endif
  1149.  
  1150.     vtputc(lchar);
  1151.     vtputc(lchar);
  1152.     vtputc(' ');
  1153.     n += 3;
  1154.     cp = &bp->b_bname[0];
  1155.  
  1156.     while ((c = *cp++) != 0) {
  1157.         vtputc(c);
  1158.         ++n;
  1159.     }
  1160.  
  1161.     vtputc(' ');
  1162.     vtputc(lchar);
  1163.     vtputc(lchar);
  1164.     n += 3;
  1165.  
  1166.     if (bp->b_fname[0] != 0) {    /* File name. */
  1167.         vtputc(' ');
  1168.         ++n;
  1169.         cp = TEXT34;
  1170. /*                   "File: " */
  1171.  
  1172.         while ((c = *cp++) != 0) {
  1173.             vtputc(c);
  1174.             ++n;
  1175.         }
  1176.  
  1177.         cp = &bp->b_fname[0];
  1178.  
  1179.         while ((c = *cp++) != 0) {
  1180.             vtputc(c);
  1181.             ++n;
  1182.             }
  1183.  
  1184.         vtputc(' ');
  1185.         ++n;
  1186.     }
  1187.  
  1188.     while (n < term.t_ncol) {    /* Pad to full width. */
  1189.         vtputc(lchar);
  1190.         ++n;
  1191.     }
  1192.  
  1193.     return (0);
  1194. }
  1195.  
  1196. PASCAL NEAR upmode()    /* update all the mode lines */
  1197.  
  1198. {
  1199.     register WINDOW *wp;
  1200.  
  1201.     wp = wheadp;
  1202.     while (wp != NULL) {
  1203.         wp->w_flag |= WFMODE;
  1204.         wp = wp->w_wndp;
  1205.     }
  1206.  
  1207.     return (0);
  1208. }
  1209.  
  1210. PASCAL NEAR upwind()    /* force hard updates on all windows */
  1211.  
  1212. {
  1213.     register WINDOW *wp;
  1214.  
  1215.     wp = wheadp;
  1216.     while (wp != NULL) {
  1217.         wp->w_flag |= WFHARD|WFMODE;
  1218.         wp = wp->w_wndp;
  1219.     }
  1220.  
  1221.     return (0);
  1222. }
  1223.  
  1224. /*
  1225.  * Send a command to the terminal to move the hardware cursor to row "row"
  1226.  * and column "col". The row and column arguments are origin 0. Optimize out
  1227.  * random calls. Update "ttrow" and "ttcol".
  1228.  */
  1229. PASCAL NEAR movecursor(row, col)
  1230.  
  1231. int row, col;
  1232.  
  1233. {
  1234.     if (row!=ttrow || col!=ttcol) {
  1235.         ttrow = row;
  1236.         ttcol = col;
  1237.         TTmove(row, col);
  1238.     }
  1239.  
  1240.     return (0);
  1241. }
  1242.  
  1243. /*
  1244.  * Erase the message line. This is a special routine because the message line
  1245.  * is not considered to be part of the virtual screen. It always works
  1246.  * immediately; the terminal buffer is flushed via a call to the flusher.
  1247.  */
  1248.  
  1249. PASCAL NEAR mlferase()
  1250.  
  1251. {
  1252.     register int save_discmd;
  1253.  
  1254.     save_discmd = discmd;
  1255.     discmd = TRUE;
  1256.     mlerase();
  1257.     discmd = save_discmd;;
  1258.  
  1259.     return (0);
  1260. }
  1261.  
  1262. PASCAL NEAR mlerase()
  1263.  
  1264. {
  1265.     int i;
  1266.     
  1267.     movecursor(term.t_nrow, 0);
  1268.     if (discmd == FALSE)
  1269.         return (0);
  1270.  
  1271. #if    COLOR
  1272.     TTforg(7);
  1273.     TTbacg(gbcolor);
  1274. #endif
  1275.  
  1276.     if (eolexist == TRUE)
  1277.         TTeeol();
  1278.     else {
  1279.         for (i = 0; i < term.t_ncol - 1; i++)
  1280.             TTputc(' ');
  1281.  
  1282.         /* force the move! */
  1283. /*        movecursor(term.t_nrow, 1);*/
  1284.         movecursor(term.t_nrow, 0);
  1285.     }
  1286.     TTflush();
  1287.     mpresf = FALSE;
  1288.  
  1289.     return (0);
  1290. }
  1291.  
  1292. /*
  1293.  * Write a message into the message line. Keep track of the physical cursor
  1294.  * position. A small class of printf like format items is handled. Assumes the
  1295.  * stack grows down; this assumption is made by the "+=" in the argument scan
  1296.  * loop. If  STACK_GROWS_UP  is set in estruct.h, then we'll assume that the
  1297.  * stack grows up and use "-=" instead of "+=". Set the "message line"
  1298.  *  flag TRUE.  Don't write beyond the end of the current terminal width.
  1299.  */
  1300.  
  1301. PASCAL NEAR mlout(c)
  1302.  
  1303. int c;    /* character to write */
  1304.  
  1305. {
  1306.     if (ttcol + 1 < term.t_ncol)
  1307.         TTputc(c);
  1308.     if (c != '\b')
  1309.         *lastptr++ = c;
  1310.     else if (lastptr > &lastmesg[0])
  1311.         --lastptr;
  1312.  
  1313.     return (0);
  1314. }
  1315.  
  1316. #if    VARARG
  1317. #if    VARG
  1318. CDECL NEAR mlwrite(va_alist)
  1319.  
  1320. va_dcl        /* variable argument list
  1321.             arg1 = format string
  1322.             arg2+ = arguments in that string */
  1323.  
  1324. {
  1325.     register int c;     /* current char in format string */
  1326.     register char *fmt;    /* ptr to format string */
  1327.     register va_list ap;    /* ptr to current data field */
  1328.     int arg_int;        /* integer argument */
  1329.     long arg_long;        /* long argument */
  1330.     char *arg_str;        /* string argument */
  1331.  
  1332.     /* if we are not currently echoing on the command line, abort this */
  1333.     if (discmd == FALSE)
  1334.         return;
  1335.  
  1336. #if    COLOR
  1337.     /* set up the proper colors for the command line */
  1338.     TTforg(7);
  1339.     TTbacg(gbcolor);
  1340. #endif
  1341.  
  1342.     /* point to the first argument */
  1343.     va_start(ap);
  1344.     fmt = va_arg(ap, char *);
  1345.  
  1346.     /* if we can not erase to end-of-line, do it manually */
  1347.     if (eolexist == FALSE) {
  1348.         mlerase();
  1349.         TTflush();
  1350.     }
  1351.  
  1352.     movecursor(term.t_nrow, 0);
  1353.      lastptr = &lastmesg[0];        /* setup to record message */
  1354.     while ((c = *fmt++) != 0) {
  1355.         if (c != '%') {
  1356.             mlout(c);
  1357.             ++ttcol;
  1358.         } else {
  1359.             c = *fmt++;
  1360.             switch (c) {
  1361.                 case 'd':
  1362.                     arg_int = va_arg(ap, int);
  1363.                     mlputi(arg_int, 10);
  1364.                     break;
  1365.  
  1366.                 case 'o':
  1367.                     arg_int = va_arg(ap, int);
  1368.                     mlputi(arg_int, 8);
  1369.                     break;
  1370.  
  1371.                 case 'x':
  1372.                     arg_int = va_arg(ap, int);
  1373.                     mlputi(arg_int, 16);
  1374.                     break;
  1375.  
  1376.                 case 'D':
  1377.                     arg_long = va_arg(ap, long);
  1378.                     mlputli(arg_long, 10);
  1379.                     break;
  1380.  
  1381.                 case 's':
  1382.                     arg_str = va_arg(ap, char *);
  1383.                     mlputs(arg_str);
  1384.                     break;
  1385.  
  1386.                 case 'f':
  1387.                     arg_int = va_arg(ap, int);
  1388.                     mlputf(arg_int);
  1389.                     break;
  1390.  
  1391.                 default:
  1392.                     mlout(c);
  1393.                     ++ttcol;
  1394.             }
  1395.         }
  1396.     }
  1397.  
  1398.     /* if we can, erase to the end of screen */
  1399.     if (eolexist == TRUE)
  1400.         TTeeol();
  1401.     TTflush();
  1402.     mpresf = TRUE;
  1403.     *lastptr = 0;    /* terminate lastmesg[] */
  1404.     va_end(ap);
  1405. }
  1406. #else
  1407. CDECL NEAR mlwrite(char *fmt, ...)
  1408. /* char * fmt;*/
  1409.  
  1410.         /* variable argument list
  1411.             arg1 = format string
  1412.             arg2+ = arguments in that string */
  1413.  
  1414. {
  1415.     register int c;     /* current char in format string */
  1416.     va_list ap;        /* ptr to current data field */
  1417.     int arg_int;        /* integer argument */
  1418.     long arg_long;        /* long argument */
  1419.     char *arg_str;        /* string argument */
  1420.  
  1421.     /* if we are not currently echoing on the command line, abort this */
  1422.     if (discmd == FALSE)
  1423.         return (0);
  1424.  
  1425. #if    COLOR
  1426.     /* set up the proper colors for the command line */
  1427.     TTforg(7);
  1428.     TTbacg(gbcolor);
  1429. #endif
  1430.  
  1431.     /* point to the first argument */
  1432.     va_start(ap, fmt);
  1433.  
  1434.     /* if we can not erase to end-of-line, do it manually */
  1435.     if (eolexist == FALSE) {
  1436.         mlerase();
  1437.         TTflush();
  1438.     }
  1439.  
  1440.     movecursor(term.t_nrow, 0);
  1441.      lastptr = &lastmesg[0];        /* setup to record message */
  1442.     while ((c = *fmt++) != 0) {
  1443.         if (c != '%') {
  1444.             mlout(c);
  1445.             ++ttcol;
  1446.         } else {
  1447.             c = *fmt++;
  1448.             switch (c) {
  1449.                 case 'd':
  1450.                     arg_int = va_arg(ap, int);
  1451.                     mlputi(arg_int, 10);
  1452.                     break;
  1453.  
  1454.                 case 'o':
  1455.                     arg_int = va_arg(ap, int);
  1456.                     mlputi(arg_int, 8);
  1457.                     break;
  1458.  
  1459.                 case 'x':
  1460.                     arg_int = va_arg(ap, int);
  1461.                     mlputi(arg_int, 16);
  1462.                     break;
  1463.  
  1464.                 case 'D':
  1465.                     arg_long = va_arg(ap, long);
  1466.                     mlputli(arg_long, 10);
  1467.                     break;
  1468.  
  1469.                 case 's':
  1470.                     arg_str = va_arg(ap, char *);
  1471.                     mlputs(arg_str);
  1472.                     break;
  1473.  
  1474.                 case 'f':
  1475.                     arg_int = va_arg(ap, int);
  1476.                     mlputf(arg_int);
  1477.                     break;
  1478.  
  1479.                 default:
  1480.                     mlout(c);
  1481.                     ++ttcol;
  1482.             }
  1483.         }
  1484.     }
  1485.  
  1486.     /* if we can, erase to the end of screen */
  1487.     if (eolexist == TRUE)
  1488.         TTeeol();
  1489.     TTflush();
  1490.     mpresf = TRUE;
  1491.     *lastptr = 0;    /* terminate lastmesg[] */
  1492.     va_end(ap);
  1493.  
  1494.     return (0);
  1495. }
  1496. #endif
  1497. #else
  1498.  
  1499. #if    STACK_GROWS_UP
  1500. #define    ADJUST(ptr, dtype)    ptr -= sizeof(dtype)
  1501. #else
  1502. #define    ADJUST(ptr, dtype)    ptr += sizeof(dtype)
  1503. #endif
  1504.  
  1505. CDECL NEAR mlwrite(fmt)
  1506.  
  1507. char *fmt;    /* format string for output */
  1508.  
  1509. {
  1510.     register int c;     /* current char in format string */
  1511.     register char *ap;    /* ptr to current data field */
  1512.  
  1513.     /* if we are not currently echoing on the command line, abort this */
  1514.     if (discmd == FALSE)
  1515.         return;
  1516.  
  1517. #if    COLOR
  1518.     /* set up the proper colors for the command line */
  1519.     TTforg(7);
  1520.     TTbacg(gbcolor);
  1521. #endif
  1522.  
  1523.     /* point to the first argument */
  1524.     ap = &fmt;
  1525.     ADJUST(ap, char *);
  1526.  
  1527.     /* if we can not erase to end-of-line, do it manually */
  1528.     if (eolexist == FALSE) {
  1529.         mlerase();
  1530.         TTflush();
  1531.     }
  1532.  
  1533.     movecursor(term.t_nrow, 0);
  1534.      lastptr = &lastmesg[0];        /* setup to record message */
  1535.     while ((c = *fmt++) != 0) {
  1536.         if (c != '%') {
  1537.             mlout(c);
  1538.             ++ttcol;
  1539.         } else {
  1540.             c = *fmt++;
  1541.             switch (c) {
  1542.                 case 'd':
  1543.                     mlputi(*(int *)ap, 10);
  1544.                             ADJUST(ap, int);
  1545.                     break;
  1546.  
  1547.                 case 'o':
  1548.                     mlputi(*(int *)ap,  8);
  1549.                     ADJUST(ap, int);
  1550.                     break;
  1551.  
  1552.                 case 'x':
  1553.                     mlputi(*(int *)ap, 16);
  1554.                     ADJUST(ap, int);
  1555.                     break;
  1556.  
  1557.                 case 'D':
  1558.                     mlputli(*(long *)ap, 10);
  1559.                     ADJUST(ap, long);
  1560.                     break;
  1561.  
  1562.                 case 's':
  1563.                     mlputs(*(char **)ap);
  1564.                     ADJUST(ap, char *);
  1565.                     break;
  1566.  
  1567.                 case 'f':
  1568.                     mlputf(*(int *)ap);
  1569.                     ADJUST(ap, int);
  1570.                     break;
  1571.  
  1572.                 default:
  1573.                     mlout(c);
  1574.                     ++ttcol;
  1575.             }
  1576.         }
  1577.     }
  1578.  
  1579.     /* if we can, erase to the end of screen */
  1580.     if (eolexist == TRUE)
  1581.         TTeeol();
  1582.     TTflush();
  1583.     mpresf = TRUE;
  1584.     *lastptr = 0;    /* terminate lastmesg[] */
  1585. }
  1586. #endif
  1587. /*    Force a string out to the message line regardless of the
  1588.     current $discmd setting. This is needed when $debug is TRUE
  1589.     and for the write-message and clear-message-line commands
  1590. */
  1591.  
  1592. PASCAL NEAR mlforce(s)
  1593.  
  1594. char *s;    /* string to force out */
  1595.  
  1596. {
  1597.     register int oldcmd;    /* original command display flag */
  1598.  
  1599.     oldcmd = discmd;    /* save the discmd value */
  1600.     discmd = TRUE;        /* and turn display on */
  1601.     mlwrite(s);        /* write the string out */
  1602.     discmd = oldcmd;    /* and restore the original setting */
  1603.  
  1604.     return (0);
  1605. }
  1606.  
  1607. /*
  1608.  * Write out a string. Update the physical cursor position. This assumes that
  1609.  * the characters in the string all have width "1"; if this is not the case
  1610.  * things will get screwed up a little.
  1611.  */
  1612.  
  1613. PASCAL NEAR mlputs(s)
  1614.  
  1615. char *s;
  1616.  
  1617. {
  1618.     register int c;
  1619.  
  1620.     while ((c = *s++) != 0) {
  1621.         mlout(c);
  1622.         ++ttcol;
  1623.     }
  1624.  
  1625.     return (0);
  1626. }
  1627.  
  1628. /*
  1629.  * Write out an integer, in the specified radix. Update the physical cursor
  1630.  * position.
  1631.  */
  1632. PASCAL NEAR mlputi(i, r)
  1633.  
  1634. int i, r;
  1635.  
  1636. {
  1637.     register int q;
  1638.     static char hexdigits[] = "0123456789ABCDEF";
  1639.  
  1640.     if (i < 0)
  1641.     {
  1642.     i = -i;
  1643.     mlout('-');
  1644.     }
  1645.  
  1646.     q = i/r;
  1647.  
  1648.     if (q != 0)
  1649.     mlputi(q, r);
  1650.  
  1651.     mlout(hexdigits[i%r]);
  1652.     ++ttcol;
  1653.  
  1654.     return (0);
  1655. }
  1656.  
  1657. /*
  1658.  * do the same except as a long integer.
  1659.  */
  1660. PASCAL NEAR mlputli(l, r)
  1661.  
  1662. long l;
  1663. int r;
  1664.  
  1665. {
  1666.     register long q;
  1667.  
  1668.     if (l < 0)
  1669.     {
  1670.     l = -l;
  1671.     mlout('-');
  1672.     }
  1673.  
  1674.     q = l/r;
  1675.  
  1676.     if (q != 0)
  1677.     mlputli(q, r);
  1678.  
  1679.     mlout((int)(l%r)+'0');
  1680.     ++ttcol;
  1681.  
  1682.     return (0);
  1683. }
  1684.  
  1685. /*
  1686.  *    write out a scaled integer with two decimal places
  1687.  */
  1688.  
  1689. PASCAL NEAR mlputf(s)
  1690.  
  1691. int s;    /* scaled integer to output */
  1692.  
  1693. {
  1694.     int i;    /* integer portion of number */
  1695.     int f;    /* fractional portion of number */
  1696.  
  1697.     /* break it up */
  1698.     i = s / 100;
  1699.     f = s % 100;
  1700.  
  1701.     /* send out the integer portion */
  1702.     mlputi(i, 10);
  1703.     mlout('.');
  1704.     mlout((f / 10) + '0');
  1705.     mlout((f % 10) + '0');
  1706.     ttcol += 3;
  1707.  
  1708.     return (0);
  1709. }       
  1710.  
  1711. #if RAINBOW
  1712.  
  1713. PASCAL NEAR putline(row, col, buf)
  1714.     int row, col;
  1715.     char buf[];
  1716.     {
  1717.     int n;
  1718.  
  1719.     n = strlen(buf);
  1720.     if (col + n - 1 > term.t_ncol)
  1721.     n = term.t_ncol - col + 1;
  1722.     Put_Data(row, col, n, buf);
  1723.     }
  1724. #endif
  1725.  
  1726.